package com.bidevalution.business.impl;

import com.bidevalution.business.context.BidType;
import com.bidevalution.business.service.BeSectionTechService;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.entity.TdAuditMethod;
import com.bidevalution.dao.entity.TdTechnology;
import com.bidevalution.dao.mapper.BeSectionEnterpriseMapper;
import com.bidevalution.dao.mapper.BeSectionInfoMapper;
import com.bidevalution.dao.mapper.TdAuditMethodMapper;
import com.bidevalution.dao.mapper.TdTechnologyMapper;
import com.qtp.core.model.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author huyiqi
 * @date 2020/3/18
 */
@Service("beSectionTechService")
public class BeSectionTechServiceImpl implements BeSectionTechService {
    @Override
    public Response<List<TdTechnology>> getTechnologyBySectionId(String sectionId) {
        BeSectionInfo sectionInfo = beSectionInfoMapper.getSectionInfoById(sectionId);
        if(sectionInfo != null) {
            String projId = sectionInfo.getTdProjectid();
            List<TdTechnology> list = tdTechnologyMapper.getTechByProjId(projId);
            return Response.ok(list);
        }
        return Response.fail("没有找到项目信息");
    }

    @Override
    public Response<List<Map<String, String>>> getEntTechScores(String sectionId, String expertId) {
        BeSectionInfo sectionInfo = beSectionInfoMapper.getSectionInfoById(sectionId);
        List<Map<String, String>> entTechScores = new ArrayList<>();
        if(sectionInfo != null) {
            String projId = sectionInfo.getTdProjectid();
            List<TdTechnology> list = tdTechnologyMapper.getTechByProjId(projId);
            List<BeSectionEnterprise> entList = beSectionEnterpriseMapper.selectBySectionId(sectionId);
            List<String> techSort = getTechSort(list);
            if(list!=null && entList!=null) {
                for(BeSectionEnterprise ent:entList) {
                    Map<String, String> entScore = getEnterTech(expertId, projId, isHidden(projId), ent, techSort);
                    entTechScores.add(entScore);
                }
            }
            return Response.ok(entTechScores);
        }
        return Response.fail("没有找到项目信息");
    }

    private Map<String, String> getEnterTech(String expertId, String projId, boolean isHidden, BeSectionEnterprise ent, List<String> techSort) {
        int techSize = techSort.size();
        String[] scores = new String[techSize];
        List<Map<String, String>> scoreList = tdTechnologyMapper.getExpertTechScores(projId, expertId, ent.getSeqId());
        if(scoreList!=null) {
            for (Map<String, String> score : scoreList) {
                String jsId = score.get("jsId");
                String s = score.get("score");
                int index = techSort.indexOf(jsId);
                scores[index] = s;
            }
        }

        String scoreStr = StringUtils.join(scores, ",");
        Map<String, String> entScores = new Hashtable<>();
        if(isHidden) {
            entScores.put(ent.getBidHideNumber(), scoreStr);
        } else {
            entScores.put(ent.getEnterpriseName(), scoreStr);
        }

        return entScores;
    }

    private List<String> getTechSort(List<TdTechnology> list) {
        List<String> sort = new ArrayList<>();
        for(TdTechnology tech:list) {
            sort.add(tech.getTechJSMethodID());
        }
        return sort;
    }

    private boolean isHidden(String projectId) {
        TdAuditMethod tdAuditMethod = tdAuditMethodMapper.getAuditMethod(projectId);
        return tdAuditMethod != null && BidType.HIDDEN.getCode().equals(tdAuditMethod.getAumeIsBidHide());
    }


    @Autowired(required = false)
    private BeSectionInfoMapper beSectionInfoMapper;
    @Autowired(required = false)
    private TdTechnologyMapper tdTechnologyMapper;
    @Autowired(required = false)
    private BeSectionEnterpriseMapper beSectionEnterpriseMapper;
    @Autowired(required = false)
    private TdAuditMethodMapper tdAuditMethodMapper;
}
